//
//  diaryTableViewController.m
//  CustomDateTimePicker
//
//  Created by lanou3g on 15/7/20.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "diaryTableViewController.h"
#import "SqliteDtaHandle.h"
#import "diaryDetaileViewController.h"
@interface diaryTableViewController ()
@property(nonatomic,retain)NSMutableArray *arr;

@end

@implementation diaryTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
   self.tableView.backgroundColor = [UIColor colorWithRed:255/256.0 green:204/256.0 blue:204/256.0 alpha:1];
    
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.navigationController.navigationBar.tintColor = [UIColor brownColor];
    //[[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"edit.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(deleteDiary:)];
    //设置返回按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"ca1.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(returnBackWrite:)];

    //属性传值
    self.arr = _dArray;
    
}

//返回按钮事件
-(void)returnBackWrite:(UIBarButtonItem *)bt{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)deleteDiary:(UIBarButtonItem *)bt{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.arr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    cell.textLabel.text = self.arr[indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.backgroundColor = [UIColor colorWithRed:255/256.0 green:204/256.0 blue:204/256.0 alpha:1];
    return cell;
}



// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}



// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // 先删除数据
        SqliteDtaHandle *sq = [SqliteDtaHandle shareSqliteDataHandle];
        [sq deletWithContact:self.arr[indexPath.row]];
        [self.arr removeObjectAtIndex:indexPath.row];
        //刷新视图
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    diaryDetaileViewController *detaileVC = [[diaryDetaileViewController alloc]init];
    detaileVC.contactStr = self.arr[indexPath.row];
    [self.navigationController pushViewController:detaileVC animated:YES];
    
}



@end
